<script setup lang="ts">
import {onMounted, ref} from "vue";
import {router} from "../../router";
import {Message, Notification} from '@arco-design/web-vue';
import myAxios from "../../plugins/myAxios.ts";
import {useUserStore} from "../../stores/user.ts";

const images = ['src/assets/carousel/soap-3123468_1280.jpg', 'src/assets/carousel/tea-2519551_1280.jpg'];

const inputValues = ref('');
const userEmail = ref('');
const showCode = ref(false);
const isSuccess = ref(false);

const loginCard = ref(null);
const passwordCard = ref(null); // 将 passwordCard 声明为 ref 对象

const status = ref('login');
const loginUser = useUserStore()

const sendCode = async () => {
  if (!userEmail.value) {
    Message.error('请输入邮箱')
  }else{
    // 切换至验证码界面
    status.value = 'code';
    isSuccess.value = true;
    Notification.success('已发送验证码')
    const res = await myAxios.get(`/user/get-code?email=${userEmail.value}`);
    if (res.code === 0) {
      showCode.value = false;
    } else {
      Notification.error('未知错误');
      isSuccess.value = true;
      showCode.value = false;
    }
  }
};

const userLogin = () => {
  isSuccess.value = false;
  myAxios.post('/user/login', {
    userEmail: userEmail.value,
    code: inputValues.value,
  }).then((response) => {
    if (response.code === 0) {
      loginUser.setLoginUser(response.data);
      Notification.success('登录成功')
      router.push('/')
    } else {
      isSuccess.value = true;
      showCode.value = false;
      Notification.error(response.message)
    }
  })
};
</script>

<template>
  <div id="login">
    <div class="left">
      <button class="back" type="button" @click="router.back()">
        <icon-import style="font-size: 24; margin-right: 10px"/>
      </button>
      <a-carousel
          :auto-play="true"
          indicator-type="dot"
          show-arrow="hover"
      >
        <a-carousel-item v-for="image in images">
          <img :src="image" class="img-carousel" alt=""/>
        </a-carousel-item>
      </a-carousel>
    </div>
    <div class="form-container">
      <form class="form">
        <svg
            xmlns="http://www.w3.org/2000/svg"
            class="lock-icon"
            width="1em"
            height="1em"
            viewBox="0 0 24 24"
            stroke-width="0"
            fill="currentColor"
            stroke="currentColor"
        >
          <path
              d="M12 2C9.243 2 7 4.243 7 7v3H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8a2 2 0 0 0-2-2h-1V7c0-2.757-2.243-5-5-5zM9 7c0-1.654 1.346-3 3-3s3 1.346 3 3v3H9V7zm4 10.723V20h-2v-2.277a1.993 1.993 0 0 1 .567-3.677A2.001 2.001 0 0 1 14 16a1.99 1.99 0 0 1-1 1.723z"
          ></path>
        </svg>
        <div v-if="status == 'login'">
          <p class="form-title">登录账号</p>
          <br>
          <p class="form-sub-title">
            很高兴见到您 在下面登录您的帐户。
          </p>
          <div ref="loginCard" class="login-card">
            <div class="field-container">
              <input v-model="userEmail" placeholder="" class="input" type="email"/>
              <span class="placeholder">Email</span>
            </div>
            <button class="btn btn-label" type="button" @click="sendCode">
              <label class="btn-label">
                <span>下一步</span>
              </label>
            </button>
          </div>
        </div>
        <div v-else>
          <p class="form-title">邮箱验证</p>
          <br>
          <p class="form-sub-title">
            很高兴再次见到您 在下面填写您的邮箱验证码。
          </p>
          <div ref="passwordCard" class="password-card">
            <div class="box">
              <span>验证码</span>
              <a-verification-code :length="4" class="input-code" v-model="inputValues" style="width: 300px" />
            </div>
            <button class="btn btn-label" type="button" @click="userLogin">
              <span v-if="!showCode && isSuccess" class="btn-label">Login</span>
              <span v-else><a-spin dot/></span>
            </button>
          </div>

          <div class="back-icon" @click="status = 'login'">
            <icon-arrow-left style="font-size: 24; margin-right: 10px"/>
          </div>
        </div>
      </form>

    </div>
  </div>
</template>

<style scoped>
#login {
  width: 100vw;
  height: 100vh;
  --s: 60px;
  --c1: #AB8460;
  --c2: #5b42f3;
  --_g: radial-gradient(
      25% 25% at 25% 25%,
      var(--c1) 99%,
      rgba(0, 0, 0, 0) 101%
  );
  background: var(--_g) var(--s) var(--s) / calc(2 * var(--s)) calc(2 * var(--s)),
  var(--_g) 0 0 / calc(2 * var(--s)) calc(2 * var(--s)),
  radial-gradient(50% 50%, var(--c2) 98%, rgba(0, 0, 0, 0)) 0 0 / var(--s) var(--s),
  repeating-conic-gradient(var(--c2) 0 50%, var(--c1) 0 100%) calc(0.5 * var(--s)) 0 / calc(2 * var(--s)) var(--s);
}

.left {
  width: 42vw;
  height: 100vh;
}

.left :deep(.arco-carousel) {
  width: 100%;
  height: 100%;
}

.img-carousel {
  width: 100%;
  height: 100%;
  object-fit: fill;
}

.back {
  position: absolute;
  top: 1vh;
  right: 54vw;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  box-shadow: rgba(100, 100, 111, 0.5) 0px 7px 29px 0px;
  background: #508eec;
  border: none;
  outline: none;
  cursor: pointer;
}

.back-icon {
  width: 50px;
  position: relative;
  left: 1rem;
  top: 5rem;
  cursor: pointer;
}

.form-container {
  width: 500px;
  height: 254px;
  background: #fff;
  border-radius: 8px;
  box-shadow: rgba(50, 50, 93, 0.25) 0px 6px 12px -2px,
  rgba(0, 0, 0, 0.3) 0px 3px 7px -3px;
  box-sizing: border-box;
  position: absolute;
  top: 35vh;
  right: 10vw;
  overflow: hidden;
}

.lock-icon {
  font-size: 20px;
  color: #0e2954;
}

.form {
  width: 100%;
  height: 100%;
  padding: 20px 15px;
  font-family: "Trebuchet MS", "Lucida Sans Unicode", "Lucida Grande",
  "Lucida Sans", Arial, sans-serif;
  box-sizing: border-box;
  color: #0e2954;
  position: relative;
}

.form-title {
  margin-top: 10px;
  margin-bottom: 5px;
  font-size: 25px;
  font-weight: 700;
  font-family: "Trebuchet MS", "Lucida Sans Unicode", "Lucida Grande",
  "Lucida Sans", Arial, sans-serif;
}

.form-sub-title {
  margin: 0;
  font-size: 15px;
  font-weight: 500;
  font-family: "Trebuchet MS", "Lucida Sans Unicode", "Lucida Grande",
  "Lucida Sans", Arial, sans-serif;
}

.login-card,
.password-card {
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  padding-top: 25px;
  gap: 20px;
  align-items: flex-end;
  position: absolute;
  width: calc(100% - 30px);
  transition: all 0.3s ease-in-out;
  bottom: 10px;
}

.login-card {
  left: 15px;
}

.password-card {
  left: 15px;
}

.field-container {
  border: 1px solid #a7a7a7;
  border-radius: 8px;
  width: 100%;
  height: 45px;
  position: relative;
  box-sizing: border-box;
  overflow: hidden;
  display: flex;
  align-items: flex-end;
  justify-content: flex-start;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.input {
  width: 100%;
  height: 100%;
  padding: 5px 10px;
  box-sizing: border-box;
  border: none;
  outline: none;
  background: transparent;
  position: relative;
  z-index: 11;
}

.box {
  position: absolute;
  bottom: 8rem;
  right: 1vw;
}

.box span {
  position: relative;
  top: 8.6rem;
  right: 6rem;
  font-size: 20px;
  font-weight: 700;
}

.input-code {
  position: relative;
  height: 3em;
  margin: 1em;
  border-radius: 5px;
  border: none;
  outline: none;
  background-color: #000000;
  top: 6.5em;
  right: 1.5em;
  padding-left: 15px;
  padding-right: 15px;
  transition: .4s ease-in-out;
}
.input-code :deep(input){
  background-color: #727a7c;
}

.input-code:hover {
  box-shadow: inset 0px 0px 0px #6f5f77,
  inset 0px 0px 0px #508eec;
  background-color: #12262c;
}

.input-code:focus {
  box-shadow: inset 0px 0px 0px #d1d1d1,
  inset 0px 0px 0px #ffffff;
  background-color: lightgrey;
}

.placeholder {
  position: absolute;
  top: 50%;
  left: 10px;
  transform: translateY(-50%);
  color: #a7a7a7;
  transition: all 0.3s ease;
  font-size: 16px;
}

.input:focus ~ .placeholder,
.input:not(:placeholder-shown) ~ .placeholder {
  top: 5px;
  transform: none;
  font-size: 12px;
}

.input:focus,
.input:not(:placeholder-shown) {
  height: 68%;
}

.btn {
  width: 100px;
  height: 40px;
  border-radius: 8px;
  box-shadow: rgba(100, 100, 111, 0.5) 0px 7px 29px 0px;
  background: #0e2954;
  border: none;
  outline: none;
}

.btn:focus {
  box-shadow: none;
}

.btn-label {
  font-size: 15px;
  font-weight: 600;
  color: #fff;
  cursor: pointer;
}

</style>
